package com.wn.service.impl;

import com.wn.entity.Book;
import com.wn.service.BookService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {
    @Override
    @Cacheable( cacheNames = "books2", key = "#id")
    public Book findById(Integer id) {

        System.out.println("查询数据库");
        return new Book(id,"java从入门到精通","张三");
    }

    @Override
    @CachePut( cacheNames = "books", key = "#book.id")
    public Book update(Book book) {
        System.out.println("更新数据库：" + book);
        return book;
    }

    @Override
    @CacheEvict( cacheNames = "books", key = "#id")
    public void delete(Integer id) {
        System.out.println("删除数据库中id为："+ id + "的书");
    }

    @Override
    @CacheEvict(cacheNames = "books*", allEntries = true)
    public void deleteAll() {
        System.out.println("删除数据库中所有的书籍");
    }
}
